<template>
  <div>
    <div class="ser_div">
      <el-row>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 产品 </el-col>
            <el-col :span="16">
              <el-select
                filterable
                v-model="ser.product_id"
                placeholder="请选择产品"
                style="width: 100%"
                @change="product_id_change"
              >
                <el-option
                  v-for="(item, index) in productList"
                  :key="index"
                  :label="item.product_name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 型号</el-col>
            <el-col :span="16">
              <el-select
                filterable
                v-model="ser.model_number_id"
                @change="model_number_id_change"
                placeholder="请选择型号"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in ModelNumberList"
                  :key="index"
                  :label="item.model_number"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 规格 </el-col>
            <el-col :span="16">
              <el-select
                filterable
                v-model="ser.specification_id"
                placeholder="请选择规格"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in SpecificationList"
                  :key="index"
                  :label="item.specification"
                  :value="item.id"
                >
                </el-option> </el-select
            ></el-col>
          </el-row>
        </el-col>
        <el-col :span="4">
          <el-button
            @click="specification_click"
            icon="el-icon-search"
            class="add_btn"
            >搜索</el-button
          >
          <el-button
            @click="specification_clickfalse"
            icon="el-icon-search"
            class="add_btn"
            >重置</el-button
          >
          <el-button
            @click="add_specification"
            icon="el-icon-plus"
            class="add_btn"
            >新增规格</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-table :data="dataList" stripe style="width: 100%" border>
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="specification" label="规格"> </el-table-column>
      <el-table-column prop="updated_at" label="时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="set_product(scope.row)" type="primary" size="mini">编辑</el-button>
          <el-button @click="get_ProductspecificationJobTypeList(scope.row)" type="primary" size="mini" >添加工价</el-button>
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="是否删除这条数据？"
            @confirm="del_product(scope.row)"
          >
            <el-button slot="reference" type="primary" size="mini" style="margin-left:10px;">
              删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        @prev-click="prev"
        @next-click="next"
        :current-page.sync="pagination.page"
        :page-size="pagination.per_page"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="isProduct"
      width="30%"
      :close-on-click-modal="false"
      @close="isProduct_close"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="产品" prop="product_id">
          <el-select
            filterable
            v-model="ruleForm.product_id"
            placeholder="请选择产品"
            style="width: 100%"
            @change="product_id_change"
          >
            <el-option
              v-for="(item, index) in productList"
              :key="index"
              :label="item.product_name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        
        <el-form-item label="型号" prop="product_id">
          <el-select
            filterable
            v-model="ruleForm.model_number_id"
            @change="model_number_id_change"
            placeholder="请选择型号"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in ModelNumberList"
              :key="index"
              :label="item.model_number"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="规格名称" prop="specification">
          <el-input
            v-model="ruleForm.specification"
            placeholder="请输入规格名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isProduct = false">取 消</el-button>
        <el-button type="primary" @click="submitadd('ruleForm')">确定</el-button>
      </span>
    </el-dialog>



    <el-dialog
      :title="title"
      :visible.sync="isjobtype"
      width="30%"
      :close-on-click-modal="false"
      @close="isProduct_close"
    >
      <div class="lis" v-for="item in ProductJobTypeList_sp" :key="item.id">
         <el-row style="display: flex;align-items:center; margin-top:5px;  justify-content: center;">
           <el-col :span="10" style="text-align:center;">
              <span>{{item.job_type}}</span>
           </el-col>
           <el-col :span="10">
              <el-input-number v-model="item.price"></el-input-number>
           </el-col>
          </el-row>
      </div>
    <span slot="footer" class="dialog-footer">
        <el-button @click="isjobtype = false">取 消</el-button>
        <el-button type="primary" @click="submitadd('ruleForm')">确定</el-button>
      </span>
    </el-dialog>


  </div>
</template>
<script>
import {
  getSpecificationList,
  addSpecification,
  deleteSpecification,
  getProductList,
  getModelNumberList,
  getJobTypePriceBySpecificationId
} from "@/api/product/index";
export default {
  data() {
    return {
      isjobtype:false,
      productList: [],
      ModelNumberList: [],
      SpecificationList: [],
      ProductJobTypeList_sp:[],
      ser: {
        product_id: "",
        model_number_id: "",
        specification_id: "",
      },
      title: "",
      isProduct: false,
      dataList: [],
      pagination: {
        per_page: 10,
        page: 1,
      },
      ids: {
        product_id:'',
        model_number_id:'',
        specification_id:''
      },
      ruleForm: {
        id: "",
        product_id:'',
        model_number_id:'',
        specification: "",
      },
      rules: {
        model_number_id:[
        {
            required: true,
            message: "请选择型号",
            trigger: "blur",
          },
        ],
        product_id:[
        {
            required: true,
            message: "请选择产品",
            trigger: "blur",
          },
        ],
        specification: [
          {
            required: true,
            message: "请输入规格名称",
            trigger: "blur",
          },
        ],
      },
      total: 0,
    };
  },
  mounted() {
    this.getlist();
    this.getProduct();
  },
  methods: {
    set_JobTypePriceBySpecificationId(){
      let obj={
        ...this.ids,
        job_type_price_arr:this.ProductJobTypeList_sp
      }
      setJobTypePriceBySpecificationId(obj).then(res=>{
        if(res.code==200){
          this.$message.success("提交成功");
          this.isjobtype=false
        }
      })
    },
    isProduct_close(){
      this.ruleForm={
        id: "",
        product_id:'',
        model_number_id:'',
        specification: "",
      }
    },
    // 查询产品规格工种列表
    get_ProductspecificationJobTypeList(product_id,model_number_id,id) {
     
      let obj = {
        product_id,
        model_number_id,
        specification_id:id,
      };
      getJobTypePriceBySpecificationId(obj).then((res) => {
        if (res.code == 200) {
          res.data.forEach((item) => {
            let obj = {
              job_type: item.job_type.job_type,
              job_type_id: item.job_type.id,
              price: item.price,
            };
            this.ProductJobTypeList_sp.push(obj);
          });
          this.isjobtype=true
        }
      });
    },
    model_number_id_change(val) {
      let obj = {
        product_id: this.ser.product_id,
        model_number_id: val,
      };
      getSpecificationList(obj).then((res) => {
        if (res.code == 200) {
          this.SpecificationList = res.data.data;
        }
      });
    },
    //获取产品
    getProduct() {
      getProductList().then((res) => {
        if (res.code == 200) {
          this.productList = res.data.data;
        }
      });
    },
    get_producModel(id) {
      getModelNumberList({ product_id: id }).then((res) => {
        if (res.code == 200) {
          this.ModelNumberList = res.data.data;
        }
      });
    },
    product_id_change(val) {
      this.ModelNumberList = [];
      this.SpecificationList = [];
      this.ser.model_number_id = "";
      this.ser.specification_id = "";
      this.get_producModel(val);
     
    },
    set_product(row) {

      this.ruleForm = row;
      this.title = "编辑规格";
      this.isProduct = true;
    },
    add_specification() {
      this.title = "新增规格";
      this.isProduct = true;
      this.$nextTick(function () {
        this.$refs.ruleForm.resetFields();
      });
    },
    submitadd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addSpecification(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.isProduct = false;
              this.$message.success("提交成功");
              this.getlist();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getlist() {
      getSpecificationList({ ...this.pagination, ...this.ser }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    specification_clickfalse() {
      this.ser = {
        product_id: "",
        model_number_id: "",
        specification_id: "",
      };
      this.getlist();
    },
    specification_click() {
      this.pagination = {
        per_page: 10,
        page: 1,
      };
      getSpecificationList({
        ...this.ser,
        ...this.pagination,
      }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist();
    },
    prev() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist();
    },
    next() {
      this.pagination.page = this.pagination.page + 1;
      this.getlist();
    },
    del_product(row) {
      deleteSpecification({ id: row.id, is_del: 1 }).then((res) => {
        if (res.code == 200) {
          this.isProduct = false;
          this.$message.success("提交成功");
          this.getlist();
        }
      });
    },
  },
};
</script>

<style></style>
